define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/order/index' + location.search,
                    edit_url: 'merchant/order/edit',
                    del_url: 'merchant/order/del',
                    multi_url: 'merchant/order/multi',
                    import_url: 'merchant/order/import',
                    table: 'merchant_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: __('User.nickname')},
                        {field: 'bshop.shopname', title: __('订货门店')},
                        {field: 'order_no',title: __('Order_no'), operate: 'LIKE',
                            formatter: function(value, row, index) {
                                return value.split(',').join('<br/>');
                        }},
                        {field: 'pay.pay_type', title: __('支付方式'), searchList: {"0":__('余额支付'),"1":__('微信支付'),"2":__('支付宝支付'),"3":__('对公转账')}, formatter: Table.api.formatter.normal},
                        {field: 'pay.pay_img', title: __('交易凭证'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_name', title: __('goods_name'), operate: false,formatter: function(value) {
                                const arr = value.split(',');
                                var html="";
                                // for (var i=0;i++;arr.length){
                                //     html +="<div>"+arr[i]+"</div>";
                                // }
                                $.each(arr,function (index,val) {
                                    console.log(val);
                                    html +='<div style="text-align: left;">'+val+'</div>';
                                });

                                return html;
                            }},
                        {field: 'address_detail', title: __('address'), operate: false,},
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"2":__('State 2'),"3":__('State 3'),"4":__('State 4'),"5":__('State 5'),"6":__('State 6'),"7":__('State 7'),"8":__('State 8'),"9":__('State 9'),"10":__('State 10'),"11":__('State 11'),"12":__('State 12'),"13":__('State 13')}, formatter: Table.api.formatter.normal},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'actual_price', title: __('Actual_price'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paymenttime', title: __('Paymenttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,

                            buttons: [
                                {
                                    name: 'detail',
                                    text: '查看/编辑',
                                    title: '查看/编辑',
                                    icon: '',
                                    extend: 'data-area=\'["90%", "90%"]\'',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: "merchant/order/detail",
                                }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'merchant/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'merchant/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'merchant/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
